package com.foo.Client;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.foo.DeCompressData.DeCompressRemote;
import com.foo.FileParser.FileParserRemote;

/**
 * Servlet implementation class ReadingFile
 */
public class ReadingFile extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReadingFile() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			String fileName = request.getParameter("fileName");
			SimpleDateFormat dateFormater = new SimpleDateFormat("yyyyMMdd");
	    	 Date startDate = dateFormater.parse(request.getParameter("startDate"));
	    	 Date endDate = dateFormater.parse(request.getParameter("endDate"));
	    	 String tickerSymbol = request.getParameter("tickerSymbol");
	    	
			Properties properties = new Properties();
			properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
			properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");			
			properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
			
			Context ctx = new InitialContext(properties);
			Object ref = ctx.lookup("FileParser/remote");
			FileParserRemote parseFile = (FileParserRemote) PortableRemoteObject.narrow(ref, FileParserRemote.class);
			String fileData = "";
			 if(tickerSymbol != null)
			 {
				 fileData = parseFile.readData(fileName, startDate, endDate, tickerSymbol);
			 }
			 else
			 {
				 fileData = parseFile.readData(fileName, startDate, endDate);
			 }
			
			String[] dataList = fileData.split(":");
	    	  for (int i = 0; i < dataList.length; i++) {
	    		  System.out.println(dataList[i]);
	  		}
	    	  
	    	PrintWriter out = response.getWriter();
	  		//html for output page
	  		out.println("<html>");
	  		out.println("<body>");
	  		out.println("<title> The requested stock data</title>");
	  		if(tickerSymbol != null && startDate != null && endDate != null) {
	  			if (fileData != null) {
	  				for (int i = 0; i < dataList.length; i++) {
	  					String line = dataList[i];
	  					out.println(line);
	  					out.println("<br>");
	  				}
	  			}
	  		}
	  		out.println("</body></html>");
			
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
	}

}
